<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Sequence;

use ArrayIterator;
use OutOfBoundsException;
use Skyline\Structure\AbstractCollection;

/**
 * 
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class ArrayList extends AbstractCollection implements ListInterface
{
	/**
	 * @var int[mixed]
	 */
	protected $elements = array();
	
	/**
	 * Class constructor
	 * 
	 * @param Traversable $elements [optional]
	 * @return void
	 */
	public function __construct($elements = null)
	{
		if(!is_null($elements)) {
			$this->addAll($elements);
		}
	}
	
	/**
	 * Add an element to the collection
	 * 
	 * @param mixed $element
	 * @return void
	 */
	public function add($element)
	{
		$this->assertNotFull();
		$this->assertElementIsValid($element);
		$this->elements[] = $element;
	}
	
	/**
	 * Adds an element to the list at a given index
	 * 
	 * @throws OutOfBoundsException If index is out of bounds (index < 0 || index > count)
	 * @param int $index
	 * @param mixed $element
	 * @return void
	 */
	public function addByIndex($index, $element)
	{
		$this->assertNotFull();
		$this->assertElementIsValid($element);
		
		if($index < 0 || $index > $this->count()) {
			throw new OutOfBoundsException(sprintf("Index %s out of bounds", $index));
		}
		
		array_splice($this->elements, $index, 0, array($element));
	}
	
	/**
	 * Adds multiple elements to the list at a given index
	 * 
	 * @throws OutOfBoundsException If index is out of bounds (index < 0 || index > count)
	 * @param int $index
	 * @param Traversable $elements
	 * @return void
	 */
	public function addAllByIndex($index, $elements)
	{
		$this->assertIsTraversable($elements);
		foreach($elements as $element) {
			$this->addByIndex($index, $element);
			$index++;
		}
	}
	
	/**
	 * Clears this container of all its elements
	 * @return void
	 */
	public function clear()
	{
		$this->elements = array();
	}
	
	/**
	 * Returns true if an element exists in the collection
	 * 
	 * @param mixed $element
	 * @return bool
	 */
	public function contains($element)
	{
		foreach($this->elements as $e) {
			if($e === $element) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns the element at a given index (if set)
	 * 
	 * @throws OutOfBoundsException If index is out of bounds (index < 0 || index >= count)
	 * @param int $index
	 * @return mixed|null Returns null if index is not set
	 */
	public function get($index)
	{
		$this->assertIndexInBounds($index);
		return $this->elements[$index];
	}
	
	/**
	 * Returns true if an element is set at a given index
	 * 
	 * @param int $index
	 * @return bool
	 */
	public function has($index)
	{
		return ($index >= 0 && $index < $this->count());
	}
	
	/**
	 * Returns the first index for a given element (if any)
	 * 
	 * @param mixed $element
	 * @return int|null Returns null if element not in list
	 */
	public function indexOf($element)
	{
		foreach($this->elements as $index => $e) {
			if($e === $element) {
				return $index;
			}
		}
	}
	
	/**
	 * Returns all indices for a given element (if any)
	 * 
	 * @param mixed $element
	 * @return int[]
	 */
	public function indicesOf($element)
	{
		$indices = array();
		foreach($this->elements as $index => $e) {
			if($e === $element) {
				$indices[] = $index;
			}
		}
		return $indices;
	}
	
	/**
	 * Removes an element from the collection
	 * 
	 * @param mixed $element
	 * @return void
	 */
	public function remove($element)
	{
		foreach($this->elements as $index => $e) {
			if($e === $element) {
				array_splice($this->elements, $index, 1);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Removes an element from the list by its index
	 * 
	 * @throws OutOfBoundsException If index is out of bounds (index < 0 || index >= count)
	 * @param int $index
	 * @return void
	 */
	public function removeByIndex($index)
	{
		$this->assertIndexInBounds($index);
		array_splice($this->elements, $index, 1);
	}
	
	/**
	 * Removes any elements in the given index range
	 * 
	 * @throws OutOfBoundsException If fromIndex is out of bounds (index < 0 || index >= count)
	 * @throws OutOfBoundsException If toIndex is out of bounds (index < 0 || index >= count)
	 * @throws OutOfBoundsException If toIndex is less than fromIndex
	 * @param int $fromIndex
	 * @param int $toIndex
	 * @return void
	 */
	public function removeByIndexRange($fromIndex, $toIndex)
	{
		$this->assertIndexInBounds($fromIndex);
		$this->assertIndexInBounds($toIndex);
		
		if($toIndex < $fromIndex) {
			throw new OutOfBoundsException('toIndex must be greater than fromIndex');
		}
		
		$length = (($toIndex - $fromIndex) + 1);
		
		array_splice($this->elements, $fromIndex, $length);
	}
	
	/**
	 * Sets an index to a given element
	 * 
	 * @throws OutOfBoundsException If index is out of bounds (index < 0 || index >= count)
	 * @param int $index
	 * @param mixed $element
	 * @return void
	 */
	public function set($index, $element)
	{
		$this->assertIndexInBounds($index);
		$this->elements[$index] = $element;
	}
	
	/**
	 * Returns this collection as a simple PHP array
	 * @return mixed[]
	 */
	public function toArray()
	{
		return array_values($this->elements);
	}
	
	/**
	 * Returns the number of elements in this list
	 * @return int
	 */
	public function count()
	{
		return count($this->elements);
	}
	
	/**
	 * Returns an iterator for the elements in this list
	 * @return Iterator
	 */
	public function getIterator()
	{
		return new ArrayIterator($this->toArray());
	}
	
	/**
	 * @throws OutOfBoundsException If index is not in the bounds of the list
	 * @param int $index
	 * @return void
	 */
	protected function assertIndexInBounds($index)
	{
		if($index < 0 || $index >= $this->count()) {
			throw new OutOfBoundsException(sprintf("Index %s out of bounds", $index));
		}
	}
}